package com.example.videolib

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.TextView
import androidx.lifecycle.Observer
import androidx.navigation.Navigation
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.baselib.activity.ChooseNickActivity
import com.example.baselib.constants.ARConstants
import com.example.baselib.constants.MyConstants
import com.example.baselib.fragment.BaseFragment
import com.example.videolib.bean.RcvData
import kotlinx.android.synthetic.main.fragment_video_main.*
import java.lang.Exception
import java.util.ArrayList

class VideoMainFragment : BaseFragment<VideoViewModel>() {
    override fun contentLayout(): Int =R.layout.fragment_video_main

    private val rcvAdapter:RcvAdapter by lazy { RcvAdapter(R.layout.rcv_item)}
    override fun providerVMClass(): Class<VideoViewModel>? =VideoViewModel::class.java
    override fun initView() {
        val empty = LayoutInflater.from(activity).inflate(R.layout.empty_item, null, false)
        empty.findViewById<TextView>(R.id.empty_msg).text="暂无数据"
        rcvAdapter.emptyView= empty
        video_main_rcv.layoutManager= LinearLayoutManager(activity, LinearLayoutManager.VERTICAL,false)
        video_main_rcv.adapter=rcvAdapter
        rcvAdapter.setOnItemClickListener { adapter, view, position ->
            val play = adapter.getItem(position) as VideoPlay
            if (  play.mPlayerStatus === VideoPlay.PlayerStatus.SUCCESS) {
                val bundle=Bundle()
                bundle.putString("cameraIndexCode", play.rcvData.cameraIndexCode)
                Navigation.findNavController(view).navigate(R.id.action_videoMainFragment_to_videoPlayFragment,bundle)
            }
        }
        if (!viewModel.getUnitInfo().isNullOrEmpty()){
            video_main_nickname.text=viewModel.getUnitInfo()["unitname"]
        }
        else if(!viewModel.getUserData().unitinfo.isNullOrEmpty()&&viewModel.getUserData().usertype==1){
            val map:HashMap<String,String> =HashMap()
            map["unitname"]=viewModel.getUserData().unitinfo[0].nickname
            map["unitcode"]=viewModel.getUserData().unitinfo[0].unitcode
            viewModel.setUnitInfo(map)
            video_main_nickname.text=viewModel.getUnitInfo()["unitname"]
        }else {
            empty.findViewById<TextView>(R.id.empty_msg).text="无煤矿信息"
            return
        }
        video_main_nickname.setOnClickListener {
            if (MyConstants.userData.usertype>1){
                startActivityForResult(Intent(activity, ChooseNickActivity::class.java),777)
            }
        }


    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, intent: Intent?) {
        super.onActivityResult(requestCode, resultCode, intent)
        if (resultCode==0&&requestCode==777){
            val map:HashMap<String,String> =HashMap()
            if (intent != null) {
                map["unitname"]= intent.getStringExtra("unitname")
                map["unitcode"]=intent.getStringExtra("unitcode")
                viewModel.setUnitInfo(map)
                viewModel.loadRcvData()
                video_main_nickname.text=map["unitname"]

            }

        }
    }
    override fun initData() {

        viewModel.getRcvData().observe(this, Observer {
            val video = ArrayList<VideoPlay>()
            for (dataBean in it) {
                video.add(VideoPlay(dataBean))
            }
            rcvAdapter.setNewData(video)
        })
    }

    override fun onStart() {
        super.onStart()
        viewModel.loadRcvData()
    }

    override fun requestError(it: Exception?) {
        video_main_empty.visibility= View.VISIBLE
        video_main_empty.findViewById<TextView>(R.id.err_msg).text=it?.message
        video_main_empty.findViewById<Button>(R.id.err_rest_btn).setOnClickListener {  viewModel.loadRcvData() }
    }



}
